﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;

namespace NewsDiscoveryEngine
{
    public class UtilityExtn
    {
        public static NewsMine.DomainObjects.FeedItem GetMergedFeedItem(FeedItem oldFeedItem, NewsMine.DomainObjects.FeedItem feedItem)
        {
           
                oldFeedItem.Link = GetBetterValue(oldFeedItem.Link, feedItem.Link);
                oldFeedItem.Domain = GetBetterValue(oldFeedItem.Domain, feedItem.Domain);
                oldFeedItem.RefUrl = GetBetterValue(oldFeedItem.RefUrl, feedItem.RefUrl);
                oldFeedItem.Rank = feedItem.Rank;
                oldFeedItem.Title = GetBetterValue(oldFeedItem.Title, feedItem.Title);
                oldFeedItem.Language = GetBetterValue(oldFeedItem.Language,feedItem.Language);
                oldFeedItem.Author = GetBetterValue(oldFeedItem.Author, feedItem.Author);
                oldFeedItem.Body = GetBetterValue(oldFeedItem.Body, feedItem.Body);
                oldFeedItem.category = GetBetterValue(oldFeedItem.category, feedItem.category);
                oldFeedItem.Description = GetBetterValue(oldFeedItem.Description, feedItem.Description);
                oldFeedItem.ImageUrl = GetBetterValue(oldFeedItem.ImageUrl, feedItem.ImageUrl);
                oldFeedItem.Keyworkds = GetBetterValue(oldFeedItem.Keyworkds, feedItem.Keyworkds);
                oldFeedItem.ThumbImageUrl = GetBetterValue(oldFeedItem.ThumbImageUrl, feedItem.ThumbImageUrl);
                oldFeedItem.ID = oldFeedItem.ID;
           

            if (oldFeedItem.Tags != null && feedItem.Tags != null)
            {
                if (oldFeedItem.Tags.Count > feedItem.Tags.Count)
                    oldFeedItem.Tags = oldFeedItem.Tags;
                else
                    oldFeedItem.Tags = feedItem.Tags;
            }
            else
            {

                if (oldFeedItem.Tags == null && feedItem.Tags != null)
                    oldFeedItem.Tags = feedItem.Tags;
                else if (oldFeedItem.Tags != null && feedItem.Tags == null)
                    oldFeedItem.Tags = oldFeedItem.Tags;
            }

            return oldFeedItem;
        }

        public static HtmlLink GetMergedHtmlLink(HtmlLink oldHtmlLink, HtmlLink htmlLink)
        {
            HtmlLink result = new HtmlLink();

            result.Link = GetBetterValue(oldHtmlLink.Link, htmlLink.Link);

            result.ImageAltText = GetBetterValue(oldHtmlLink.ImageAltText, htmlLink.ImageAltText);
            result.ImageUrl = GetBetterValue(oldHtmlLink.ImageUrl, htmlLink.ImageUrl);
            result.OuterHtml = GetBetterValue(oldHtmlLink.OuterHtml, htmlLink.OuterHtml);
            result.Title = GetBetterValue(oldHtmlLink.Title, htmlLink.Title);
            result.HtmlXpath = GetBetterValue(oldHtmlLink.HtmlXpath, htmlLink.HtmlXpath);
            result.PubDate = GetBetterValue(oldHtmlLink.PubDate, htmlLink.PubDate);

            return result;
        }

        public static HtmlLink MergeMultipleHtmlLinks(List<HtmlLink> htmlLinks)
        {
            HtmlLink result = new HtmlLink();

            for (int i = 0; i < htmlLinks.Count; i++)
            {
                result = GetMergedHtmlLink(result, htmlLinks[i]);
            }

            return result;
        }

        public static FeedItem MergeMultipleFeedItems(List<FeedItem> feedItems)
        {
            FeedItem result = new FeedItem();

            for (int i = 0; i < feedItems.Count; i++)
            {
                result = GetMergedFeedItem(result, feedItems[i]);
            }

            return result;
        }


        private static string GetBetterValue(string p, string p_2)
        {
            if (string.IsNullOrEmpty(p) && string.IsNullOrEmpty(p_2))
                return null;

            if (!string.IsNullOrEmpty(p) && string.IsNullOrEmpty(p_2))
                return p;

            if (string.IsNullOrEmpty(p) && !string.IsNullOrEmpty(p_2))
                return p_2;

            if (!string.IsNullOrEmpty(p) && !string.IsNullOrEmpty(p_2))
            {
                if (p.Length > p_2.Length)
                    return p;
                else
                    return p_2;
            }

            return p;
        }

        private static DateTime GetBetterValue(DateTime p, DateTime p_2)
        {
            if (p > p_2)
                return p;
            else
                return p_2;
        }


        //todo:This method needs togo some common place as utility method.
        public static string GetDomainNameFromUrl(string url)
        {
            Uri baseUri = new Uri(url);
            return baseUri.DnsSafeHost;
        }

        public static string GetRootDomainNameFromUrl(string url)
        {
            Uri baseUri = new Uri(url);
            string domain =  baseUri.DnsSafeHost;

            return GetRootDomainFromDomain(domain);
        }

        private static string GetRootDomainFromDomain(string domain)
        {
            //lets split the domain by '.' symbol.

            //todo: need to do some research on this and find it.

            return domain;
        }


        public static FeedItem CleanFeed(FeedItem feedItem)
        {
            if ((feedItem.Tags != null) && (feedItem.Tags.Count > 0))
            {
                feedItem.Tags = feedItem.Tags.Distinct<string>().ToList<string>();
            }
            feedItem.Tags = (from tag in feedItem.Tags select tag.Trim().Replace(" ", "-")).ToList<string>();
            if (!(!string.IsNullOrEmpty(feedItem.ThumbImageUrl) || string.IsNullOrEmpty(feedItem.ImageUrl)))
            {
                feedItem.ThumbImageUrl = feedItem.ImageUrl;
            }

            // Just adjust the image stuff.
            if (string.IsNullOrEmpty(feedItem.ImageUrl) && !string.IsNullOrEmpty(feedItem.ThumbImageUrl))
                feedItem.ImageUrl = feedItem.ThumbImageUrl;

            if (string.IsNullOrEmpty(feedItem.ThumbImageUrl) && !string.IsNullOrEmpty(feedItem.ImageUrl))
                feedItem.ThumbImageUrl = feedItem.ImageUrl;

            if (!string.IsNullOrEmpty(feedItem.ThumbImageUrl))
            {
                feedItem.ThumbImageUrl = GetThumbImageUrl(feedItem.ThumbImageUrl);
            }

            return feedItem;
        }

        private static string GetThumbImageUrl(string p)
        {
            return p; // todo: need to implemente image resizing to bring down the size that we want.

        }

    }
}
